<?php
/*****************************************************
 文件名称：comment_recent_cache
 文件作用：最新评论、留言，并使用ajax效果
******************************************************/

//ajax后台执行的函数
function comment_recent_navi(){
	if( isset($_POST['action'])&& $_POST['action'] == 'comment_recent_navi'){
		//$comment_recent_navi = get_query_var('comment_recent_navi');
		$comment_recent_navi=$_GET['comment_recent_navi'];
		//echo $comment_recent_navi;
		echo get_comment_recent($comment_recent_navi);
		die();
	}else{
		return;
	}
}
add_action('template_redirect', 'comment_recent_navi');


//显示缓存后的最新留言
function dh_comment_recent(){
 $cached = get_option('dh_comment_recent');//从options表中获取已缓存的最新留言
  $cached = get_comment_recent();
     if($cached){    //如果最新留言已缓存，直接显示，否则获取最新留言并缓存到options表中。
         echo $cached; 
     }else{
         $cached = set_comment_recent();
         echo $cached;
     }
 }

 //获取最新留言
 function get_comment_recent($page=1) {
    $comment_post_ID=0; 
	$get_total=5; 
	$show_author_url=1;
	$limit_b = ($page-1)*$get_total;
	global $wpdb;
	$request="SELECT comment_ID,comment_post_ID, comment_content, comment_author_email, comment_author, comment_author_url, comment_date FROM $wpdb->comments where comment_approved = '1' and comment_author <> 'DH' ORDER BY comment_ID DESC LIMIT $limit_b,$get_total";
	$comments=$wpdb->get_results($request);
	$before='<li>';
	$after='</li>';
    $output .= '<ul>'."\n";	
	if ($comments) {
		foreach ($comments as $comment) {
			$email=$comment->comment_author_email;
			$grav_url="http://www.gravatar.com/avatar/".md5($email)."?s=40&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2fad516503a11cd5ca435acc9bb6523536%3fs%3D32";
			$comment_author=stripslashes(strip_tags($comment->comment_author));
			$comment_content=utf8Substr(stripslashes(strip_tags($comment->comment_content)),0,150);
			$comment_content = str_replace("\"", "'", $comment_content);
			if ($show_author_url == 1 && strlen($comment->comment_author_url) > 12) $comment_author='<a target="_blank" rel="nofollow" title="访问 '.$comment_author. '的网站" href="' . $comment->comment_author_url . '">' . $comment_author . '</a>';
			$comment_date=date("m-d H:i",strtotime($comment->comment_date));
			$permalink=get_permalink($comment->comment_post_ID)."#comment-".$comment->comment_ID;
               
			$output .= $before."\n";
			$output .= '<span class="avatar"><img src="'.$grav_url.'" alt="" /></span>'."\n";
			$output .= '<span class="fn">'.$comment_author.'</span>'."\n";
			$output .= '<span class="date">'.$comment_date.'</span>'."\n";
			$output .= '<span class="txt"><a href="'.$permalink.'" title="'.$comment_content.'">'.$comment_content.'</a></span>'."\n";
			$output .= $after."\n";
		}
	}	
	$output .=  '<li class="last"><div class="comment_recent_navi">';
	$url = get_option('siteurl');
	$page_p = $page-1;
	$page_n = $page+1;
	if($page_p!=0)
	{
		$output .=  '<a class="left" href="'.$url.'/?comment_recent_navi=' .$page_p .'">上一页</a>';
	}
	$output .=  '<a class="right" href="'.$url.'/?comment_recent_navi=' .$page_n .'">下一页</a>';
	$output .=  '</div> </li>';	
	$output .=  '</ul>'."\n"; 
    return $output;
 }
 
//中文工具箱的函数，用于砍掉汉字截断尾巴可能存在的乱码。
function utf8Substr($str,$from,$len)
{ 
 return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s','$1',$str); 
}

//缓存到options表中
function set_comment_recent()
{
	$output = get_comment_recent();
    update_option('dh_comment_recent', $output); //更新缓存在options表中的最新留言
	return $output;
}

//通过此添加此Hook以保证最新留言能够被缓存到options表中。
add_action('comment_post', 'set_comment_recent');
?>